package com.zzptc.zzsgz.bdsjws.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.fragments.SelectContactFragment;
import com.zzptc.zzsgz.bdsjws.fragments.ShowAllContectFragment;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ContactActivity extends BaseActivity {
    ShowAllContectFragment showAllContectFragment;//第一个碎片
    SelectContactFragment selectfragmnent;
    @BindView(R.id.et_selectcontact)

    public EditText etSelectcontact;
    boolean flag = true;
    List<Contact> list;
    List<Contact> selectlist = new ArrayList<Contact>();
    @BindView(R.id.tv_add)
    TextView tvAdd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getContentXML() {
        return R.layout.activity_contact;
    }

    @Override
    public void initData() {
//  在碎片中绑定数据源     由内容提供者
        //  List<Contact> list= MyUtils.getAllContact();
        //RecycleView绑定
        //用碎片替换它
        tvAdd.setText("("+MyApplication.getSoslist().size()+")");
        list = MyApplication.getList();//获得所有联系人
        if (showAllContectFragment == null) {
            showAllContectFragment = new ShowAllContectFragment();
        }
        getFragmentManager().beginTransaction().replace(R.id.fl_show_contact, showAllContectFragment).addToBackStack("contact").commit();
    }

    public void onBackPressed() {
        // super.onBackPressed);
        if (getFragmentManager().getBackStackEntryCount() > 1) {
            getFragmentManager().popBackStack();
        } else {
            finish();
        }
    }

    @Override
    public void intView() {
        //监听事件
        etSelectcontact.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                // Toast.makeText(ContactActivity.this, "======", Toast.LENGTH_SHORT).show();
                if (flag) {
                    if (selectfragmnent == null) {
                        selectfragmnent = new SelectContactFragment();
                    }
                    //碎片替换
                    getFragmentManager().beginTransaction().add(R.id.fl_show_contact, selectfragmnent).hide(showAllContectFragment).addToBackStack("contact").commit();
                    flag = false;
                }

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //项 适配器 都可以用
                final CharSequence c = charSequence = charSequence.toString().replace(" ", "").replace("-", "");
                selectlist.clear();
                //耗时操作放入线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int j = 0; j < list.size(); j++) {
                            Contact contact = list.get(j);
                            if (contact.getName().contains(c) || contact.getTelnum().toString().replace(" ", "").replace("-", "").contains(c)) {
                                //产生一个新的集合 要是为真 则匹配上 如果为假 就没有匹配上
                                selectlist.add(contact);

                            }

                        }
                        //循环完毕后更新第二个碎片
                        //selectfragmnent.getSelectContactList(selectlist);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //循环完毕后更新第二个碎片
                                selectfragmnent.getSelectContactList(selectlist);
                            }
                        });
                    }
                }).start();


            }

            @Override
            public void afterTextChanged(Editable editable) {
                //当删除完毕 要不就回退到前页， 要不就显示所有联系人
                if (etSelectcontact.getText().length() == 0) {
                    if (getFragmentManager().getBackStackEntryCount() > 1) {
                        getFragmentManager().popBackStack();
                    }
                    flag = true;//开关再次打开
                }

            }
        });
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        startActivity(new Intent(this, OneKeySOSActivity.class));
    }
}
